const utils = {
    setCookie(key, value, option) {
        var str = `${key}=${encodeURIComponent(value)}`
        if (option) {
            if (option.path) {
                str += `;path=${option.path}`
            }
            if (option.expires) {
                var date = new Date()
                date.setDate(date.getDate() + option.expires)
                str += `;expires=${date.toUTCString()}`
            }
        }
        document.cookie = str
    },
    //获取cookie
    getCookie(key) {
        var str = document.cookie
        var cookie = str.split(';')
        var obj = {}
        cookie.forEach(cookie => {
            var arr = cookie.split('=')
            obj[arr[0]] = decodeURIComponent(arr[1])
        })
        return key ? obj[key] : obj
    },
    //删除cookie
    removeCookie(key, path) {
        var date = new Date()
        date.setDate(date.getDate() - 1)
        var str = `${key}=0;expires=${getDate.toUTCString()}`
        if (path) {
            str += `;path=${path}`
        }
        document.cookie = str
    },
    //ajax的get请求
    /**
     * ajax的get请求
     * @param {string}     url    请求的地址
     * @param {object}     query  请求携带的参数
     * @param {function}   fn     成功之后的回调函数
     * @param {boolean}    isJson 返回的数据是否为json格式，默认为true
     */
    get(url, query, fn, isJson = true) {
        if (query) {
            url += '?'
            for (var key in query) {
                url += `${key}=${query[key]}&`
            }
            url = url.slice(0, -1)
        }

        var xhr = new XMLHttpRequest()
        xhr.open('get', url)
        xhr.send()
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4) {
                if (xhr.status === 200) {
                    var data = isJson ? JSON.parse(xhr.responseText) : xhr.responseText
                    fn && fn(data)
                }
            }
        }
    },

    //post请求
    post(url, query, fn, isJson = true) {
        var str = ''
        if (query) {
            for (var key in query) {
                str += `${key}=${query[key]}&`
            }
            str = str.slice(0, -1)
        }
        var xhr = new XMLHttpRequest()
        xhr.open('post', url)
        // 设置请求头，在open之后send之前
        xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
        xhr.send(str)
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4) {
                if (xhr.status === 200) {
                    var data = isJson ? JSON.parse(xhr.responseText) : xhr.responseText
                    fn && fn(data)
                }
            }
        }
    },
    jsonp(url, cbName, query) {
        // 创建script
        url += `?cb=${cbName}`
        if (query) {
            for (var key in query) {
                url += `&${key}=${query[key]}`
            }
        }
        var script = document.createElement('script')
        script.src = url
        document.body.appendChild(script)
        // 由于script请求时异步的，所以只要存在就行，请求发出去之后就可以移除了
        document.body.removeChild(script)
    },
    fetch(url, query, isJson = true) {
        if (query) {
            url += `?`
            for (var key in query) {
                url += `${key}=${query[key]}&`
            }
            url.slice(0, -1)
        }
        return new Promise((resolve, reject) => {
            var xhr = new XMLHttpRequest
            xhr.open('get', url)
            xhr.send()
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4) {
                    if (xhr.status === 200) {
                        var data = isJson ? JSON.parse(xhr.responseText) : xhr.responseText
                        resolve(data)
                    } else {
                        reject()
                    }
                }
            }

        })
    }
}